﻿using FoodModel;
using J.FoodManageGet.Application.Command.DishesCommand;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageGet.Application.CommandHandler.DishesCommandHandler
{
    /// <summary>
    /// 菜品显示
    /// </summary>
    public class DishesShowCommandHandler : IRequestHandler<DishesShowCommand, (int totalCount, int PagCount, List<DishesModel> list)>
    {
        private readonly IDishesReposittory _dishesReposittory;

        public DishesShowCommandHandler(IDishesReposittory dishesReposittory)
        {
            _dishesReposittory = dishesReposittory;
        }
        public async Task<(int totalCount, int PagCount,List<DishesModel> list)> Handle(DishesShowCommand request, CancellationToken cancellationToken)
        {
            var list = await _dishesReposittory.GetAsync(x =>
           (string.IsNullOrEmpty(request.DishesName) || x.DishesName.Contains(request.DishesName))
            && (request.Inventory != 0 || x.Inventory == request.Inventory)
            && (request.Selling == 5 || x.Selling == request.Selling));
          
            int PageTotal = list.Count;
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.DishId).Skip((request.PageIndedx - 1) * request.PageSize).Take(request.PageSize).ToList();
            return (PageTotal, PageCount, list);
        }
    }
}
